//
//  MZFormatterDemoView.m
//  MZBaseDemo
//
//  Created by Chris Yang on 2016/12/6.
//  Copyright © 2016年 Chris Yang. All rights reserved.
//

#import "MZDemoSupprot.h"
#import "UIView+MZLayout.h"

#import "MZFormatterItemView.h"

#import "MZFormatterDemoView.h"

@implementation MZFormatterDemoView

- (id)init {
    
    self = [super init];
    
    if (self) {
        [self configSelf];
        [self allocSubviews];
        [self configSubviews];
        [self positionSubviews];
    }
    
    return self;
}

- (void)configSelf {
    [self setBackgroundColor:[UIColor yellowColor]];
}

- (void)allocSubviews {
    
    _scrollView = [MZDemoSupprot mzDemoScrollView];
    _baseValField = [MZDemoSupprot mzDemoField];
    _ceilValField = [MZDemoSupprot mzDemoField];
    _phoneNumFiled = [MZDemoSupprot mzDemoField];
    _randButton = [MZDemoSupprot mzDemoButton];
    _updateButton = [MZDemoSupprot mzDemoButton];
}

- (void)configSubviews {
    
    /* Scroll View */
    [_scrollView addGestureRecognizer:[self tapGesture]];
    
    /* Base Val Field */
    [_baseValField setText:@"33832549.59286"];
    [_baseValField setTextAlignment:NSTextAlignmentCenter];
    
    /* Ceil Val Field */
    [_ceilValField setText:@"897832149.67836"];
    [_ceilValField setTextAlignment:NSTextAlignmentCenter];
    
    /* Phone Number Field */
    [_phoneNumFiled setText:@"18611112222"];
    [_phoneNumFiled setTextAlignment:NSTextAlignmentCenter];
    
    /* Rand Button */
    [_randButton setTitle:@"生成随机值" forState:UIControlStateNormal];
    
    /* Update Button */
    [_updateButton setTitle:@"展示更新" forState:UIControlStateNormal];
    
    /* Item Array */
    
    [self addSubview:_scrollView];
    [_scrollView addSubview:_baseValField];
    [_scrollView addSubview:_ceilValField];
    [_scrollView addSubview:_phoneNumFiled];
    [_scrollView addSubview:_randButton];
    [_scrollView addSubview:_updateButton];
    for (UIView *tmpView in _itemViewArray) {
        [_scrollView addSubview:tmpView];
    }
}

- (void)positionSubviews {
    
    CGRect tmpRect = CGRectZero;
    float tmpSpace = 15;
    float tmpWidth = self.bounds.size.width - 2 * tmpSpace;
    float tmpHalfWidth = (self.bounds.size.width - 3 * tmpSpace) / 2;
    float tmpHeight = 40;
    
    /* Scroll View */
    tmpRect = self.bounds;
    [_scrollView setFrame:tmpRect];
    
    /* Base Val Field */
    tmpRect = [_scrollView mzRectInSubviewWithWidth:tmpHalfWidth height:tmpHeight toLeft:tmpSpace toTop:80];
    [_baseValField setFrame:tmpRect];
    
    /* Ceil Val Field */
    tmpRect = [_scrollView mzRectInSubviewWithWidth:tmpHalfWidth height:tmpHeight toRight:tmpSpace toTop:80];
    [_ceilValField setFrame:tmpRect];

    /* Phone Number Field */
    tmpRect = [_scrollView mzCenterRectInSubviewWithWidth:tmpWidth height:tmpHeight];
    tmpRect.origin.y = [_baseValField mzGetYForBrotherViewToTopWithSpace:tmpSpace];
    [_phoneNumFiled setFrame:tmpRect];
    
    /* Rand Button */
    tmpRect = [_scrollView mzRectInSubviewWithWidth:tmpHalfWidth height:50 toLeft:tmpSpace toTop:80];
    tmpRect.origin.y = [_phoneNumFiled mzGetYForBrotherViewToTopWithSpace:tmpSpace];
    [_randButton setFrame:tmpRect];

    /* Update Button */
    tmpRect = [_scrollView mzRectInSubviewWithWidth:tmpHalfWidth height:50 toRight:tmpSpace toTop:80];
    tmpRect.origin.y = _randButton.frame.origin.y;
    [_updateButton setFrame:tmpRect];

    /* Item Array */
    tmpRect = [_scrollView mzCenterRectInSubviewWithWidth:tmpWidth height:tmpHeight];
    tmpRect.origin.y = [_randButton mzGetYForBrotherViewToTopWithSpace:tmpSpace];
    for (MZFormatterItemView *tmpView in _itemViewArray) {
        [tmpView setFrame:tmpRect];
        tmpRect.origin.y += (tmpHeight + tmpSpace);
    }
    
    /* Scroll View ContentSize */
    tmpRect.size.width = _scrollView.bounds.size.width;
    tmpRect.size.height = MAX(tmpRect.origin.y, _scrollView.bounds.size.height + 1);
    [_scrollView setContentSize:tmpRect.size];
}

- (void)layoutSubviews {
    [self positionSubviews];
}


#pragma mark - Touch

- (UIGestureRecognizer *)tapGesture {
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTapped)];
    return tapGesture;
}

- (void)scrollViewTapped {
    [self endEditing:YES];
}

#pragma mark - Target-Action Pair

- (void)buttonClicked:(UIButton *)button {
    
}

#pragma mark - MISC

#pragma mark - User Interface

- (void)resetItemWithTitleArray:(NSArray<NSString *> *)titleArray {
    
    for (UIView *tmpView in _itemViewArray) {
        [tmpView removeFromSuperview];
    }
    
    NSMutableArray *tmpMulArray = [[NSMutableArray alloc] init];
    for (NSString *tmpStr in titleArray) {
        
        MZFormatterItemView *tmpView = [[MZFormatterItemView alloc] init];
        [tmpView updateTitle:tmpStr];
        [tmpMulArray addObject:tmpView];
    }
    
    _itemViewArray = [NSArray arrayWithArray:tmpMulArray];
    
    [self configSubviews];
    [self positionSubviews];
}

- (void)updateValue:(NSString *)valueStr atIndex:(NSInteger)index {

    if (index < 0 || index >= _itemViewArray.count) {
        return;
    }
    
    MZFormatterItemView *tmpView = _itemViewArray[index];
    [tmpView updateValue:valueStr];
}

@end
